package components
{
	import SEApp.dialogs.se_propertyDialog;
	
	import UIFree.ObjectEvent;
	import UIFree.components.uif_window;
	import UIFree.uif_component;
	
	import classes.clt_events;
	
	import components.plugins.effects.clt_baseEffect;
	import components.plugins.effects.clt_effectLayer;
	
	import flash.events.Event;
	
	public class clt_propertyLayer extends clt_component {
		private var _curEffect	: clt_baseEffect;
		private var _dialog		: se_propertyDialog;
		public function clt_propertyLayer(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:String='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(clt_events.EFFECTPARAMS, doEffectParams, workspace);
			addEvent(clt_effectLayer.EFFECTSELECT, doEffectSelect, workspace);
		}
		
		private function doEffectSelect(e: ObjectEvent): void {
			if (_dialog) _dialog.close();
		}
		
		private function doEffectParams(e: ObjectEvent): void {
			_curEffect = e.object as clt_baseEffect;
			if (_dialog) {
				_dialog.close(0, function(): void {
					doEffectParams(e);
				});
			} else {
				if (_curEffect) {
					var params: Array = [];
					var values: Array = _curEffect.values;
					_curEffect.paramName.forEach(function(a_title: String, i: int, arr: Array): void {
						if (a_title)
							params.push({
								index	: i,
								title	: a_title,
								type	: se_propertyDialog.PTYPE_NUMBER,
								value	: values[i]
							});
					});
					_dialog = new se_propertyDialog(clt.dialogLayer, params, clt.locale.PROPERTYTITLE, false, 2, 300);
					_dialog.addEvent(uif_window.CLOSEWINDOW, function(e: Event): void {
						_dialog = null;
					});
					_dialog.addEvent(Event.CHANGE, doChange);
				}
			}
		}
		
		protected function doChange(e: Event): void {
			var values: Array = _curEffect.values;
			var setParams: Array = _dialog.values as Array;
			for (var i: int=0; i<setParams.length; i++) {
				values[setParams[i].index] = setParams[i].value;
			}
			_curEffect.values = values;
			clt.workspace.dispatchEvent(new Event(clt_events.DISPLAYREFRESH));
		}
	}
}